package com.pmgames.service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.pmgames.dao.PmgamesBaseService;
import com.pmgames.vo.Content;
import com.pmgames.vo.GameObject;
import com.pmgames.vo.IXmlSerializable;
import com.pmgames.vo.User;

public class LoadGameService extends PmgamesBaseService {

	Content word;
	
	public LoadGameService() {
		// TODO Auto-generated constructor stub
	}
	
	public Content getWord() {
		return word;
	}

	public void setWord(Content word) {
		this.word = word;
	}

	@Override
	protected String getXmlFromOperation(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		/*Document doc = getXmlFromRequest(request);
		
		doc.getDocumentElement().normalize();
		NodeList nodeList = doc.getElementsByTagName("user");
		
		Node nNode = nodeList.item(0);
		if (nNode.getNodeType() == Node.ELEMENT_NODE) 
		{
			 
			Element eElement = (Element) nNode;
			int id = Integer.parseInt(getTagValue("id",eElement));
			
	  		GameObject game = getPmgamesDao().loadGame(id);
	  		if(game == null)
	  			throw new Exception("Invalid game id - load failed.");
	  		Content word = getPmgamesDao().getWord(game);
	  		return game.toXml() + word.toXml();
		}		
		return null;*/
		
		int gameId = Integer.parseInt(request.getParameter("id"));
		GameObject game = getPmgamesDao().loadGame(gameId);
		if(game == null)
  			throw new Exception("Invalid game id - load failed.");
		Content word = getPmgamesDao().getWord(game);
  		setWord(word);
  		//encodeWord
  		return game.toXml() + word.encodeWord().toXml();
	}

}
